package com.guanyu.u2cache.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/11/15 11:21
 **/
public class TimeUtil {

    /**
     * @param timeUnit
     * @return
     */
    public static TemporalUnit timeUnitToChron(TimeUnit timeUnit) {
        if (timeUnit.equals(TimeUnit.DAYS)) {
            return ChronoUnit.DAYS;
        }
        if (timeUnit.equals(TimeUnit.HOURS)) {
            return ChronoUnit.HOURS;
        }
        if (timeUnit.equals(TimeUnit.MINUTES)) {
            return ChronoUnit.MINUTES;
        }
        return ChronoUnit.SECONDS;
    }

    public static String getCurrentTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }

    /**
     * 将事件转换为秒
     *
     * @param timeUnit
     * @param time
     * @return
     */
    public static Long timeTransToSeconds(TimeUnit timeUnit, long time) {
        long multiplier = 1;
        if (timeUnit.equals(TimeUnit.MINUTES)) {
            multiplier = 60L;
        } else if (timeUnit.equals(TimeUnit.HOURS)) {
            multiplier = 60L * 60;
        } else if (timeUnit.equals(TimeUnit.DAYS)) {
            multiplier = 60L * 60 * 24;
        }
        return time * multiplier;
    }
}
